using Microsoft.AspNetCore.Components;
using System.Net.Http.Json;

namespace FastapiBlazorBlog.Pages
{

    public class DetailBase : ComponentBase
    {
        [Inject]
        public HttpClient Http { get; set; }

        [Inject]
        public Microsoft.JSInterop.IJSRuntime JSRuntime { get; set; }

        /// <summary>
        /// 接收id路径参数
        /// </summary>
        [Parameter]
        public int Id { get; set; }



        public class RespData<T> where T : new()
        {
            public int Code { get; set; }
            public string Msg { get; set; }
            public T Data { get; set; } = new T();
        }

        public class Category
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

        public class ArticleDto
        {
            public int Id { get; set; }
            public string Title { get; set; }
            public string Description { get; set; }
            public string Content { get; set; }
            public int Created_at { get; set; }
            public int Updated_at { get; set; }
            public Category Category { get; set; }
        }

        public ArticleDto article = new ArticleDto();



        protected override async Task OnInitializedAsync()
        {
            var artRes = await Http.GetFromJsonAsync<RespData<ArticleDto>>($"/api/article/{Id}");
            article = artRes.Data;
            await base.OnInitializedAsync();
        }

        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            // 调用js进行代码高亮
            CodeHighlight highlight = new CodeHighlight(JSRuntime);
            await highlight.UseHighlight();

            await base.OnAfterRenderAsync(firstRender);
        }
    }
}
